<?php namespace Phpcmf\Controllers;

class Show extends \Phpcmf\Home\Module
{

    public function index() {
        $this->_module_init();
        $this->_Show(
            (int)\Phpcmf\Service::L('Input')->get('id'),
            [
                'field' => dr_safe_replace(\Phpcmf\Service::L('Input')->get('field')),
                'value' => dr_safe_replace(\Phpcmf\Service::L('Input')->get('value')),
            ],
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }
	
	

    public function time() {
        $this->_module_init();
        $this->_MyShow(
            'time',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    public function recycle() {
        $this->_module_init();
        $this->_MyShow(
            'recycle',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    public function draft() {
        $this->_module_init();
        $this->_MyShow(
            'draft',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    public function verify() {
        $this->_module_init();
        $this->_MyShow(
            'verify',
            (int)\Phpcmf\Service::L('Input')->get('id'),
            max(1, (int)\Phpcmf\Service::L('Input')->get('page'))
        );
    }

    /**
     * 加入服务
     */
    public function cart() {

        !$this->uid && $this->_json(0, dr_lang('还没有登录'));

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        !$id && $this->_json(0, dr_lang('id参数不完整'));

        $this->tablename = SITE_ID.'_'.APP_DIR;
        $data = \Phpcmf\Service::M()->db->table($this->tablename.'_index')->where('id', $id)->countAllResults();
        !$data && $this->_json(0, dr_lang('模块内容不存在'));

        $cart = \Phpcmf\Service::M()->db->table($this->tablename.'_cart')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
        if ($cart) {
            // 已经退出了,我们就删除它
            \Phpcmf\Service::M()->db->table($this->tablename.'_cart')->where('id', intval($cart['id']))->delete();
            $msg = dr_lang('退出服务');
            $c = 0;
        } else {
            \Phpcmf\Service::M()->db->table($this->tablename.'_cart')->insert(array(
                'cid' => $id,
                'uid' => $this->uid,
                'read_name' => '',
                'read_url' => '',
            ));
            $msg = dr_lang('加入服务');
            $c = 1;
        }
		// 更新数量
		$c = \Phpcmf\Service::M()->db->table($this->tablename.'_cart')->where('cid', $id)->countAllResults();
		\Phpcmf\Service::M()->db->table($this->tablename.'')->where('id', $id)->set('joins', $c)->update();
		\Phpcmf\Service::M('cache')->update_data_cache();
        // 返回结果
        $this->_json(1, $msg, $c);
    }
	/**
	 * 点赞服务
	 */
	public function fuwuzan() {
	
	    !$this->uid && $this->_json(0, dr_lang('还没有登录'));
	
	    $id = (int)\Phpcmf\Service::L('Input')->get('id');
	    !$id && $this->_json(0, dr_lang('id参数不完整'));
	
	    $this->tablename = SITE_ID.'_'.APP_DIR;
	    $data = \Phpcmf\Service::M()->db->table($this->tablename.'_index')->where('id', $id)->countAllResults();
	    !$data && $this->_json(0, dr_lang('模块内容不存在'));
	
	    $zan = \Phpcmf\Service::M()->db->table($this->tablename.'_zan')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
	    if ($zan) {
	        // 已经取消了,我们就删除它
	        \Phpcmf\Service::M()->db->table($this->tablename.'_zan')->where('id', intval($zan['id']))->delete();
	        $msg = dr_lang('取消点赞');
	        $c = 0;
	    } else {
	        \Phpcmf\Service::M()->db->table($this->tablename.'_zan')->insert(array(
	            'cid' => $id,
	            'uid' => $this->uid,
	            'read_name' => '',
	            'read_url' => '',
	        ));
	        $msg = dr_lang('点赞服务');
	        $c = 1;
	    }
		// 更新数量
		$c = \Phpcmf\Service::M()->db->table($this->tablename.'_zan')->where('cid', $id)->countAllResults();
		\Phpcmf\Service::M()->db->table($this->tablename.'')->where('id', $id)->set('fuwuzan', $c)->update();
		\Phpcmf\Service::M('cache')->update_data_cache();
	    // 返回结果
	    $this->_json(1, $msg, $c);
	}

    public function add_cart() {

        !$this->uid && $this->_json(0, dr_lang('还没有登录'));

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        !$id && $this->_json(0, dr_lang('id参数不完整'));

        $sid = (int)\Phpcmf\Service::L('Input')->get('sid');
        !$sid && $this->_json(0, dr_lang('sid参数不完整'));

        $this->tablename = SITE_ID.'_'.APP_DIR;
        $data = \Phpcmf\Service::M()->table($this->tablename.'_form_son')->where('id', $sid)->getRow();
        !$data && $this->_json(0, dr_lang('服务内容不存在'));

        $cart = \Phpcmf\Service::M()->db->table($this->tablename.'_cart')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
        if ($cart) {
            \Phpcmf\Service::M()->table($this->tablename.'_cart')->update($cart['id'], array(
                'read_name' => $data['title'],
                'read_url' => dr_son_url($data),
            ));
            $this->_json(1, 'ok');
        }

        $this->_json(0, dr_lang('未加入服务'));
    }
}
